<template>
	<view class="normal-login-container align-center" :style="{'padding-top':statusBarHeight + 'px'}">
		<view class="logo-content">
			<image :src="globalConfig.appInfo.logo" mode="widthFix">
			</image>
			<text class="title"> {{appName}}</text>
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20"
					@keyup.enter="handleLogin" />
			</view>
			<view class="input-item flex align-center" v-if="captchaEnabled != 'off'">
				<view class="iconfont icon-code icon"></view>
				<input v-model="loginForm.code" class="input" placeholder="请输入验证码" maxlength="6"
					@keyup.enter="handleLogin" />
				<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCodeImg
	} from '@/api/login'
	import configService from '@/api/config.service.js'
	export default {
		data() {
			return {
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: true,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "admin",
					password: "123456",
					code: '',
					uuid: '',
					clientId: ''
				},
				redirectUrl: '/pages/index'
			}
		},
		computed: {
			appName() {
				return uni.getSystemInfoSync().appName
			},
			statusBarHeight() {
				return uni.getSystemInfoSync().statusBarHeight
			},
		},
		onLoad(e) {
			// #ifdef H5
			if (e && e.redirectUrl) {
				this.redirectUrl = e.redirectUrl
			}
			// #endif
		},
		created() {
			this.getCode()
			// #ifdef H5
			// 绑定监听事件
			window.addEventListener("keydown", this.keyDown);
			// #endif
		},
		methods: {
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.data.captchaOff
					if (this.captchaEnabled != 'off') {
						this.codeUrl = 'data:image/gif;base64,' + res.data.img
						this.loginForm.uuid = res.data.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
				} else if (this.loginForm.code === "" && this.captchaEnabled != 'off') {
					this.$modal.msgError("请输入验证码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch(() => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch(this.redirectUrl)
				})
			},
			// 点击回车键登录
			keyDown(e) {
				// 回车则执行登录方法 enter键的ASCII是13
				if (e.keyCode === 13) {
					this.handleLogin(); // 定义的登录方法
				}
			}
		},
		destroyed() {
			// #ifdef APP-PLUS
			// 销毁事件
			window.removeEventListener("keydown", this.keyDown, false);
			// #endif
		},
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		flex-direction: column;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 5vh;

			image {
				border-radius: 4px;
				width: 100rpx;
				height: 100rpx;
				vertical-align: middle;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}
		}

		.easyinput {
			width: 100%;
		}
	}

	.login-code-img {
		height: 45px;
	}

	.other-login {
		padding: 5vh 0;
		display: flex;

		.wxLoginBtn {
			background: transparent !important;
			border: none;
		}

		.wxLoginBtn::after {
			border: none;
		}

		.loginType {
			display: flex;
			// padding: 140rpx 0;
			justify-content: center;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: skyblue;
				font-size: 22rpx;
			}
		}
	}

	.icon-login {
		font-size: 32px;
	}
</style>